#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

class St
{
public:
    string name;
    int score;

    void print()
    {
        cout << name << " " << score << endl;
    }
};

bool compare(St& a, St& b)
{
    if (a.score == b.score)
        return a.name < b.name;
    return a.score > b.score;
}

int main()
{
    int n;
    cin >> n;
    vector<St> arr(n);
    for (int i = 0; i < n; i++)
        cin >> arr[i].name >> arr[i].score;
    sort(arr.begin(), arr.end(), compare);
    for (int i = 0; i < n; i++)
    {
        arr[i].print();
    }
}